package com.first.dao.impl;

import com.first.dao.MenuDao;
import com.first.entity.Menu;
import com.first.util.DBUtil;
import com.first.util.Result;
import com.first.util.Tree;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Repository;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class MenuDaoImpl implements MenuDao {
    @Override
    public List<Menu> getAllMenuById(Integer rid) {
        String sql="SELECT tm.mid,tm.mname,tm.mcode,tm.murl,tm.mpid,tm.micon from t_role_premission trp INNER JOIN t_menu tm ON trp.mid=tm.mid where tm.mpid = 0 and trp.rid=?";
        List<Map> list = DBUtil.queryListMap(sql, rid);
        ArrayList<Menu> menus = new ArrayList<>();
        if(list.size()>0){
            for (Map map : list) {
                Menu menu = new Menu();
                try {
                    BeanUtils.populate(menu,map);
                    String sonSql="SELECT tm.mid,tm.mname,tm.mcode,tm.murl,tm.mpid,tm.micon from t_role_premission trp INNER JOIN t_menu tm ON trp.mid=tm.mid where trp.rid=? and tm.mpid=?";
                    List<Menu> sonMenus = new ArrayList<>();
                    List<Map> list1 = DBUtil.queryListMap(sonSql, rid, map.get("mid"));
                    if(list1.size()>0){
                        for (Map map1 : list1) {
                            Menu sonMenu = new Menu((Integer) map1.get("mid"), (String) map1.get("mname"), (String) map1.get("murl"));
                            sonMenus.add(sonMenu);
                        }
                    }
                    menu.setSonMenu(sonMenus);
                    menus.add(menu);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return menus;
    }

    @Override
    public List<Tree> getAllMenu() {
        String sql="select tm.mid,tm.mname from t_menu tm where tm.mpid=0 ";
        List<Map> list = DBUtil.queryListMap(sql);
        ArrayList<Tree> trees = new ArrayList<>();
        if(list.size()>0){
            for (Map map : list) {
                Tree tree = new Tree(Integer.parseInt(map.get("mid").toString()),(String)map.get("mname"),null);
                String sonSql="select mid,mname from t_menu tm where tm.mpid=?";
                List<Map> list1 = DBUtil.queryListMap(sonSql, map.get("mid"));
                ArrayList<Tree> sonTrees = new ArrayList<>();
                if(list1.size()>0){
                    for (Map map1 : list1) {
                        Tree tree1 = new Tree(Integer.parseInt(map1.get("mid").toString()),(String)map1.get("mname"),null);
                        sonTrees.add(tree1);
                    }
                }
                tree.setChildren(sonTrees);
                trees.add(tree);
            }
        }
        return trees;
    }

    @Override
    public List getAllsonMenuById(Integer rid) {
        String sql="select tm.mid from t_role_premission trp inner join t_menu tm on trp.mid=tm.mid where trp.rid=? and tm.mpid!=0";
        List<List> lits =DBUtil.queryList(sql,rid);
        return lits;
    }

    @Override
    public int editPer(Integer rid, Integer[] midArr) {
        String delSql="delete from t_role_premission where rid=?";
        int i= -1;
        int update = DBUtil.update(delSql, rid);
        for (Integer mid : midArr) {
            String insertSql="insert into t_role_premission values(null,?,?)";
            i = DBUtil.update(insertSql, rid, mid);
        }
        return i;
    }


}
